﻿using System;
using System.Collections.Generic;
using System.Text;
using NormalTests.AdventureWorks;
using Normal;
using System.Data.SqlClient;
using NUnit.Framework;
using NormalTests;
using System.Data;

namespace NormalTests
{
    [TestFixture]
    public class Program
    {
        public static void Main(string[] args)
        {
            Program prm = new Program();
            prm.TestFindById();
            prm.TestFindAll();
            prm.TestFindByCriteria();
            prm.TestDelete();
            prm.TestUpdate();
            prm.TestUpdateRequired();
            prm.TestInsertSequence();
            prm.TestFKOnDemmand();
            prm.TestFKOnDemandUpdate();
            Console.ReadLine();
        }

        [Test]
        public void TestInsertSequence()
        {
            Culture ci = new Culture();
            DAO<Culture> dao = new DAO<Culture>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Console.WriteLine("TestInsertSequence()");
            ci.Cultureid = "";
            ci.Modifieddate = DateTime.Today;
            ci.Name = "Wonderland";
            dao.Insert(ci);
        }

        [Test]
        public void TestUpdateRequired()
        {
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Console.WriteLine("TestUpdateRequired()");
            Contact item = dao.FindById(157);
            item.Firstname = null;
            dao.Update(item);
        }

        [Test]
        public void TestUpdate()
        {
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Console.WriteLine("TestUpdate()");
            Contact item = dao.FindById(157);
            item.Firstname = "Algum Mané";
            dao.Update(item);
        }

        [Test]
        public void TestDelete()
        {
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Console.WriteLine("TestDelete()");
            dao.Delete(157);
        }

        [Test]
        public void TestFindById()
        {
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Console.WriteLine("TestFindById()");
            Contact contact = dao.FindById(157);
            Console.WriteLine(contact.Contactid + ", " + contact.Firstname + " " + contact.Lastname);
        }

        [Test]
        public void TestFindAll()
        {
            long PageNumber = 2;
            long PageSize = 20;
            long NumberOfPages = 0;
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            List<Contact> lst = dao.FindAll(PageNumber, PageSize, out NumberOfPages);
            Console.WriteLine("TestFindAll() => " + NumberOfPages);
            foreach (Contact contact in lst)
            {
                Console.WriteLine(contact.Contactid + ", " + contact.Firstname + " " + contact.Lastname);
            }
        }

        [Test]
        public void TestFindByCriteria()
        {
            DAO<Contact> dao = new DAO<Contact>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            List<string> Criteria = new List<string>();
            Criteria.Add("FirstName like 'Gu%'");
            Criteria.Add("EmailPromotion >= 0");
            List<Contact> lst = dao.FindByCriteria(Criteria);
            Console.WriteLine("TestFindAll()");
            foreach (Contact contact in lst)
            {
                Console.WriteLine(contact.Contactid + ", " + contact.Firstname + " " + contact.Lastname);
            }
        }

        [Test]
        public void TestFKOnDemmand()
        {
            DAO<Employee> dao = new DAO<Employee>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            List<Employee> empls = dao.FindAll();
            foreach (Employee empl in empls)
            {
                Console.WriteLine("Employee: " + empl.Title);
                if (empl.Contactid != null)
                {
                    Console.WriteLine("\t" + empl.Contactid.Firstname + " " + empl.Contactid.Lastname);
                }
                if (empl.Managerid != null)
                {
                    Console.WriteLine("\tManaged By: " + empl.Managerid.Contactid.Firstname + " " + empl.Managerid.Contactid.Lastname);
                }
                else
                {
                    Console.WriteLine("\tNo Managed");
                }
            }
        }

        [Test]
        public void TestFKOnDemandUpdate()
        {
            DAO<Employee> dao = new DAO<Employee>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Employee empl = dao.FindById(32);
            empl.Gender = "F";
            dao.Update(empl);
        }

        [Test]
        public void TestAutoNumberNative()
        {
            DAO<Department> dao = new DAO<Department>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            List<string> Criteria = new List<string>();
            Criteria.Add("Name Like 'Stuff%'");
            List<Department> lst = dao.FindByCriteria(Criteria);
            foreach (Department delpt in lst)
            {
                dao.Delete(delpt.Departmentid);
            }
            Department dept = new Department();
            dept.Name = "Stuff";
            dept.Groupname = "GRP";
            dept.Modifieddate = DateTime.Today;
            dao.Insert(dept);
            Console.WriteLine("New ID generated: " + dept.Departmentid);
            dept.Name = "Stuff 2";
            dao.Update(dept);
        }

        [Test]
        public void TestDepartmentHistory()
        {
            DAO<Department> dao = new DAO<Department>("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, DatabaseType.MsSQL);
            Department dept = dao.FindById(7);
            foreach (Employeedepartmenthistory hist in dept.Employeedepartmenthistorys)
            {
                Console.WriteLine(hist.Employeeid.Contactid.Firstname + " " + hist.Employeeid.Contactid.Lastname);
            }
        }

        [Test]
        public void TestNormalDump()
        {
            NormalDump dmp = new NormalDump("data source=" +
                Settings.Default.Server + ";initial catalog=" +
                Settings.Default.Database + ";user id=" +
                Settings.Default.User + ";password=" +
                Settings.Default.Password, "System.Data.SqlClient");
            dmp.Generate(@"c:\temp", true, "", "Teste", false);
        }

        [Test]
        public void TestArbitrayCommand()
        {
            string strCnn ="data source=" +
               Settings.Default.Server + ";initial catalog=" +
               Settings.Default.Database + ";user id=" +
               Settings.Default.User + ";password=" +
               Settings.Default.Password;
            DAO<Employee> dao = new DAO<Employee>(strCnn, "System.Data.SqlClient");
            SqlConnection cnn = new SqlConnection(strCnn);
            cnn.Open();
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from HumanResources.Employee Where Year(HireDate)=@HireYear";
            cmd.Parameters.Add(new SqlParameter("@HireYear", 2000));
            
            List<Employee> lst = dao.ExecuteCommandList(cmd);

            foreach (Employee emp in lst)
            {
                Console.WriteLine(emp.Contactid.Firstname + " " + emp.Contactid.Lastname);
            }
        }
    }
}